<?php

namespace app\model;

use app\api\controller\Base;
use think\facade\Db;
use think\Model;

class PostCommentsModel extends Model {

    protected $table ="post_comments";

    protected $pk = 'comment_id';

    private $cacheKey="comment:id:";

    private $cacheTime=51840000;


    /*
   * 获取total
   * */
    public function getTotal($where=[]){
        return Db::name($this->table)->where($where)->count();
    }


    /*
     * 获取list
     * */
    public function getList($where,$page = null,$limit = null,$orderFields = []){

        $obj =  Db::name($this->table)->field($this->pk)->where($where);

        if(!empty($field) ) {
            $obj = $obj->field($field);
        }

        if(!empty($page) && !empty($limit)){
            $obj = $obj->page($page, $limit);
        }

        if(!empty($orderFields)){
            $obj = $obj->order($orderFields);
        }

        $list = $obj->select();

        if($list){
            $return = [];
            foreach($list as $item){
                $return[] = $this->getInfo($item[$this->pk]);
            }
            return $return;
        }
        return [];
    }



    /*
* 获取订单状态详情
* */
    public function getInfo($id){
        $cacheKey = $this->cacheKey.$id;
        $redisModel = new RedisModel();
        $info = $redisModel->get($cacheKey);
        if($info){
            $info =  json_decode($info,true);
        }else{
            $info = Db::name($this->table)->find($id);
            $postInfo = (new PostsModel())->getInfo($info["post_id"]);
            $info["isAuthor"] = $postInfo["uid"] == $info["uid"] ? 1 : 0;
            $info["user"] = (new UserModel())->getUserBaseInfoByUid($info["uid"]);
            $info["images"] = !empty($info["images"]) ? json_decode($info["images"], true) : [];
            $redisModel->set($cacheKey,json_encode($info,256),$this->cacheTime);
        }
        $uid = getUid();
        $info["haveLike"] = $uid ? (new PostUserLikesModel())->isPostCommentLike($uid,$id) : 0;
        $where=[];
        $where[] = ['parent_comment_id','=',$id];
        $info["childList"] = $this->getList($where,1,5,["create_at"=>"DESC","likes"=>"DESC"]);
        $info["childTotal"] = $this->getTotal($where);
        return $info;
    }


    public function incrField($id,$field,$num = 1){
        if(empty($id) ){
            return false;
        }

        $where =[];
        $where[] =[$this->pk,"=",$id] ;
        return Db::name($this->table)->where($where)->inc($field,$num)->update();
    }


    public function decField($id,$field,$num = 1){
        if(empty($id) ){
            return false;
        }

        $where =[];
        $where[] =[$this->pk,"=",$id] ;
        return Db::name($this->table)->where($where)->dec($field,$num)->update();
    }


    /*
    * 保存数据
    * */
    public function addOne($data){
        return Db::name($this->table)->insertGetId($data);
    }

    /*
    * 修改运单号
    * */
    public function updateOne($id, $data){
        $where[] = [$this->pk,"=",$id];
        return Db::name($this->table)->where($where)->update($data);
    }


    public function delCache($id)
    {
        $redisModel = new RedisModel();

        return $redisModel->delete($this->cacheKey.$id);
    }


    public function delAllCache()
    {
        $redisModel = new RedisModel();

        return $redisModel->deleteAll($this->cacheKey);
    }

}